package car.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import dhu.com.car.common.R;
import dhu.com.car.entity.User;
import dhu.com.car.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public R<User> login(HttpServletRequest request, @RequestBody User user){
        /**
         * 1根据页面提交的用户名username查询数据库
         * 2如果没有查询到则返回登陆失败结果
         * 3密码比对，如果不一致则返回登陆失败结果
         * 4查看员工状态，如果已禁用状态，则返回员工禁用结果
         * 6登陆成功，将员工id存入Session并返回登陆成功结果
         */
        String password = user.getPassword();

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getName,user.getName()).eq(User::getPassword,password);
        User emp = userService.getOne(queryWrapper);

        if(emp==null){
            return R.error("登陆失败");
        }

        if(!emp.getPassword().equals(password)){
            return R.error("登陆失败");
        }


        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);

    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){

        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 新增员工
     * @param
     * @return
     */
    @PostMapping
    public R<String> save (HttpServletRequest request,@RequestBody User user){



        userService.save(user);

        return R.success("新增员工成功");
    }

    /**
     * 注册
     * @param
     * @return
     */
    @PostMapping("/save")
    public R<String> registe (@RequestBody User user){

        String username = user.getName();
        user.setState(0);
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getName,username);
        User one = userService.getOne(wrapper);
        if(one!=null){
            return R.error("用户名已存在");
        }

        userService.save(user);
        return R.success("新增员工成功");
    }

    /**
     * 员工信息的分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){

        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);

        //构造条件构造器
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.eq(User::getState,1);
        queryWrapper.like(StringUtils.isNotEmpty(name),User::getName,name);
        //排序条件
        queryWrapper.orderByDesc(User::getName);

        //执行查询
        userService.page(pageInfo,queryWrapper);


        return R.success(pageInfo);
    }

    /**
     * 用户信息的分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize, String name){

        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);

        //构造条件构造器
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.eq(User::getName,0);
        if(StringUtils.isNotEmpty(name)){
            queryWrapper.and(wapper->{
                wapper.like(User::getName,name).or().like(User::getName,name);
            });
        }


        //执行查询
        userService.page(pageInfo,queryWrapper);


        return R.success(pageInfo);
    }

    /**
     * 根据id修改员工信息
     * @param
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody User user){

//        Long empId=(Long)request.getSession().getAttribute("employee");
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(empId);
        userService.updateById(user);
        return R.success("员工信息修改成功");
    }

    /**
     * 根据员工id查询信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<User> getById(@PathVariable("id") Long id){
        User employee=userService.getById(id);
        if(employee!=null){
            return R.success(employee);
        }
        return R.error("没有查询到对应员工信息");
    }
}

